﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//-------------------------------------------------------------------------------------

using System.Data;

namespace DotNet.Business
{
    using DotNet.BaseManager;
    using DotNet.DbUtilities;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// BaseModulePermissionManager
    /// 模块权限关系管理
    ///
    /// 修改记录
    ///
    ///     2008.06.19 版本：1.0 JiRiGaLa 创建主键。
    ///
    /// 版本：1.0
    /// </summary>
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2008.06.19</date>
    /// </author>
    /// </summary>
    public class BaseModulePermissionManager : BaseManager
    {
        public BaseModulePermissionManager()
        {
            base.CurrentTableName = BasePermissionTable.TableName;
        }

        public BaseModulePermissionManager(IDbHelper dbHelper)
            : this()
        {
            DbHelper = dbHelper;
        }

        public BaseModulePermissionManager(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            UserInfo = userInfo;
        }

        public string[] GetModuleIds(string permissionItemId)
        {
            string[] returnValue = null;
            string[] names = new string[2];
            string[] values = new string[2];
            names[0] = BasePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BasePermissionTable.FieldPermissionId;
            values[1] = permissionItemId;
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionTable.FieldResourceId);
            return returnValue;
        }

        public string[] GetPermissionIds(string moduleId)
        {
            string[] returnValue = null;
            string[] names = new string[2];
            string[] values = new string[2];
            names[0] = BasePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BasePermissionTable.FieldResourceId;
            values[1] = moduleId;
            DataTable dataTable = this.GetDT(names, values);
            returnValue = BaseBusinessLogic.FieldToArray(dataTable, BasePermissionTable.FieldPermissionId);
            return returnValue;
        }

        #region public int AddEntity(BasePermissionEntity resourcePermission) 添加实体

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="resourcePermission">实体对象</param>
        /// <returns>影响行数</returns>
        public int AddEntity(BasePermissionEntity resourcePermission)
        {
            // 是否已经指定了ID，为了提高批量增加数据的运行速度
            SQLBuilder sqlBuilder = new SQLBuilder(DbHelper);
            sqlBuilder.BeginInsert(BasePermissionTable.TableName);
            sqlBuilder.SetValue(BasePermissionTable.FieldResourceId, resourcePermission.ResourceId);
            sqlBuilder.SetValue(BasePermissionTable.FieldResourceCategory, resourcePermission.ResourceCategory);
            sqlBuilder.SetValue(BasePermissionTable.FieldCreateUserId, UserInfo.Id);
            sqlBuilder.SetValue(BasePermissionTable.FieldPermissionId, resourcePermission.PermissionId);
            sqlBuilder.SetDBNow(BasePermissionTable.FieldCreateOn);
            return sqlBuilder.EndInsert();
        }

        #endregion public int AddEntity(BasePermissionEntity resourcePermission) 添加实体

        public int Add(string moduleId, string permissionItemId)
        {
            int returnValue = 0;
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BasePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BasePermissionTable.FieldResourceId;
            values[1] = moduleId;
            names[2] = BasePermissionTable.FieldPermissionId;
            values[2] = permissionItemId;

            // 检查记录是否重复
            if (!this.Exists(names, values))
            {
                BasePermissionEntity resourcePermission = new BasePermissionEntity();
                resourcePermission.ResourceId = moduleId;
                resourcePermission.ResourceCategory = "Module";
                resourcePermission.PermissionId = int.Parse(permissionItemId);
                returnValue = this.AddEntity(resourcePermission);
            }
            return returnValue;
        }

        public int Add(string[] moduleIds, string permissionItemId)
        {
            int returnValue = 0;
            for (int i = 0; i < moduleIds.Length; i++)
            {
                this.Add(moduleIds[i], permissionItemId);
                returnValue++;
            }
            return returnValue;
        }

        public int Add(string moduleId, string[] permissionItemIds)
        {
            int returnValue = 0;
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                this.Add(moduleId, permissionItemIds[i]);
                returnValue++;
            }
            return returnValue;
        }

        public int Add(string[] moduleIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            for (int i = 0; i < moduleIds.Length; i++)
            {
                for (int j = 0; i < permissionItemIds.Length; i++)
                {
                    this.Add(moduleIds[i], permissionItemIds[j]);
                    returnValue++;
                }
            }
            return returnValue;
        }

        public int Delete(string moduleId, string permissionItemId)
        {
            string[] names = new string[3];
            string[] values = new string[3];
            names[0] = BasePermissionTable.FieldResourceCategory;
            values[0] = "Module";
            names[1] = BasePermissionTable.FieldResourceId;
            values[1] = moduleId;
            names[2] = BasePermissionTable.FieldPermissionId;
            values[2] = permissionItemId;
            return this.BatchDelete(names, values);
        }

        public int Delete(string[] moduleIds, string permissionItemId)
        {
            int returnValue = 0;
            for (int i = 0; i < moduleIds.Length; i++)
            {
                returnValue += this.Delete(moduleIds[i], permissionItemId);
            }
            return returnValue;
        }

        public int Delete(string moduleId, string[] permissionItemIds)
        {
            int returnValue = 0;
            for (int i = 0; i < permissionItemIds.Length; i++)
            {
                returnValue += this.Delete(moduleId, permissionItemIds[i]);
            }
            return returnValue;
        }

        public int Delete(string[] moduleIds, string[] permissionItemIds)
        {
            int returnValue = 0;
            for (int i = 0; i < moduleIds.Length; i++)
            {
                for (int j = 0; i < permissionItemIds.Length; i++)
                {
                    returnValue += this.Delete(moduleIds[i], permissionItemIds[j]);
                }
            }
            return returnValue;
        }
    }
}